﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ORM.Business.Models;
using ORM.Common.Helpers;
using ORM.Business.Interfaces;
using ORM.Business.Models.Entities;
using ORM.Common;
using ORM.Framework.Filters;
using ORM.ViewModels.Questions;
using System.Configuration;
using ORM.Web.Models;

namespace ORM.Controllers
{
    public class QuestionController : OrmControllerBase
    {
        private IQuestionRepository _iQuestionRepository;
        private IModelRepository _iModelRepository;
        private IDeviceRepository _iDeviceRepository;
        private IManufacturerRepository _iManufacturerRepository;
        private IAnswerRepository _iAnswerRepository;
        private IGuideRepository _iGuideRepository;
        private OrmEntities ORMDbContext = new OrmEntities();
        public QuestionController(IQuestionRepository iQuestionRepository, IModelRepository iModelRepository, IDeviceRepository iDeviceRepository, IManufacturerRepository iManufacturerRepository, IAnswerRepository iAnswerRepository, IGuideRepository iGuideRepository)
        {
            _iQuestionRepository = iQuestionRepository;
            _iModelRepository = iModelRepository;
            _iDeviceRepository = iDeviceRepository;
            _iManufacturerRepository = iManufacturerRepository;
            _iAnswerRepository = iAnswerRepository;
            _iGuideRepository = iGuideRepository;
        }

        //
        // GET: /Question/

        public ViewResult Index(string condition, int pageNo = 1, int deviceId = 0, int manufacturerId = 0, int modelId = 0, string stringToSearch = "")
        {
            List<Question> questionList = new List<Question>();
            ViewData["Device_Name"] = new SelectList(ORMDbContext.Devices.Where(i => !i.IsDeleted), "Id", "Name", null).OrderBy(d => d.Text);
            ViewData["Manufacturer_Name"] = new SelectList(ORMDbContext.Manufacturers.Where(i => !i.IsDeleted), "Id", "Name", null).OrderBy(m => m.Text);
            ViewData["Model_Name"] = new SelectList(ORMDbContext.Models.Where(i => !i.IsDeleted && i.DeviceId == deviceId && i.ManufacturerId == manufacturerId), "Id", "Name", null).OrderBy(m => m.Text);
            ViewBag.StringToSearch = stringToSearch;
            ViewBag.DeviceId = deviceId;
            if (deviceId != 0)
            {
                ViewBag.DeviceName = ORMDbContext.Devices.Find(deviceId).Name;
            }

            ViewBag.ManufacturerId = manufacturerId;
            if (manufacturerId != 0)
            {
                ViewBag.ManufacturerName = ORMDbContext.Manufacturers.Find(manufacturerId).Name;
            }
            ViewBag.ModelId = modelId;
            if (modelId != 0)
            {
                ViewBag.ModelName = ORMDbContext.Models.Find(modelId).Name;
            }
            return View(questionList);
        }

        //-----------------------------------------------------------------------------------------------------
        //hungnm
        //public ActionResult GetAllUser(string QuestionSearch)
        //{
        //    //var feedback = _userRepository.GetAllUsers();
        //    //if (feedback.Success)
        //    //{
        //    //    return View(feedback.Data);
        //    //}

        //    //return View(new List<user>());
        //    QuestionListViewModel vm = new QuestionListViewModel();

        //    var feedback = _iQuestionRepository.GetSimilarQuestions(QuestionSearch);

        //    if (feedback.Success)
        //    {
        //        vm.QuestionSearch = QuestionSearch;
        //        vm.Questions = feedback.Data;
        //        //foreach (var user in vm.Users)
        //        //{
        //        //    user.UserState = ReferenceDataCache.UserStateCollection.FirstOrDefault(i => i.Id == user.UserStateId);
        //        //}
        //    }
        //    else
        //    {
        //        SetMessage(feedback.Message, MessageType.Error);
        //    }
        //    return View("_QuestionList",vm);

        //}
        //-----------------------------------------------------------------------------------------------------
        public ActionResult GetQuestionList(string condition, int pageNo = 1, int deviceId = 0, int manufacturerId = 0, int modelId = 0, string stringToSearch = "")
        {
            QuestionListViewModel vm = null;
            List<Question> questionList = new List<Question>();
            var pageInfo = new PageInfo(Convert.ToInt16(ConfigurationManager.AppSettings["QuestionsPageSize"]), pageNo);           
            if (condition == "new")
            {
                var feedback = _iQuestionRepository.GetAllQuestion(pageInfo, deviceId, manufacturerId, modelId, stringToSearch);
                if (feedback.Success)
                {
                    vm = new QuestionListViewModel
                    {
                        Questions = feedback.Questions,
                        PageNo = pageNo,
                        Condition = "new",
                        DeviceId = deviceId,
                        ManufacturerId = manufacturerId,
                        ModelId = modelId,
                        QuestionSearch = stringToSearch,
                        TotalPage = Convert.ToInt16(Math.Ceiling((double)feedback.Total / pageInfo.PageSize)),
                    };
                    return PartialView("_QuestionList", vm);
                }
            }
            else if (condition == "helpful")
            {
                var feedback = _iQuestionRepository.GetHelpfulQuestion(pageInfo, deviceId, manufacturerId, modelId, stringToSearch);
                if (feedback.Success)
                {
                    vm = new QuestionListViewModel
                    {
                        Questions = feedback.Questions,
                        PageNo = pageNo,
                        Condition = "helpful",
                        DeviceId = deviceId,
                        ManufacturerId = manufacturerId,
                        ModelId = modelId,
                        QuestionSearch = stringToSearch,
                        TotalPage = Convert.ToInt16(Math.Ceiling((double)feedback.Total / pageInfo.PageSize)),
                    };
                    return PartialView("_QuestionList", vm);
                }
            }
            else if (condition == "resolved")
            {
                var feedback = _iQuestionRepository.GetResolvedQuestion(pageInfo, deviceId, manufacturerId, modelId, stringToSearch);
                if (feedback.Success)
                {
                    vm = new QuestionListViewModel
                    {
                        Questions = feedback.Questions,
                        PageNo = pageNo,
                        Condition = "resolved",
                        DeviceId = deviceId,
                        ManufacturerId = manufacturerId,
                        ModelId = modelId,
                        QuestionSearch = stringToSearch,
                        TotalPage = Convert.ToInt16(Math.Ceiling((double)feedback.Total / pageInfo.PageSize)),
                    };
                    return PartialView("_QuestionList", vm);
                }
            }
            else if (condition == "unresolved")
            {
                var feedback = _iQuestionRepository.GetUnresolvedQuestion(pageInfo, deviceId, manufacturerId, modelId, stringToSearch);
                if (feedback.Success)
                {
                    vm = new QuestionListViewModel
                    {
                        Questions = feedback.Questions,
                        PageNo = pageNo,
                        Condition = "unresolved",
                        DeviceId = deviceId,
                        ManufacturerId = manufacturerId,
                        ModelId = modelId,
                        QuestionSearch = stringToSearch,
                        TotalPage = Convert.ToInt16(Math.Ceiling((double)feedback.Total / pageInfo.PageSize)),
                    };
                    return PartialView("_QuestionList", vm);
                }
            }
            else if (condition == "unanswered")
            {
                var feedback = _iQuestionRepository.GetUnansweredQuestion(pageInfo, deviceId, manufacturerId, modelId, stringToSearch);
                if (feedback.Success)
                {
                    vm = new QuestionListViewModel
                    {
                        Questions = feedback.Questions,
                        PageNo = pageNo,
                        Condition = "unanswered",
                        DeviceId = deviceId,
                        ManufacturerId = manufacturerId,
                        ModelId = modelId,
                        QuestionSearch = stringToSearch,
                        TotalPage = Convert.ToInt16(Math.Ceiling((double)feedback.Total / pageInfo.PageSize)),
                    };
                    return PartialView("_QuestionList", vm);
                }
            }
            return PartialView("_QuestionList");
        }

        public ActionResult GetAnswerList(int questionId, string condition, int pageNo = 1)
        {
            AnswerListViewModel vm = null;
            List<Answer> answerList = new List<Answer>();
            var canAccept = false;
            if(CurrentUser != null)
            {
                canAccept = _iQuestionRepository.CheckIfAnUserCanAcceptAnswerForAQuestion(questionId, CurrentUser.UserInfo.Id);
            }
            int currentUserId = CurrentUser == null ? 0 : Convert.ToInt16(CurrentUser.UserInfo.Id);    
            var pageInfo = new PageInfo(Convert.ToInt16(ConfigurationManager.AppSettings["AnswersPageSize"]), pageNo);
            if (condition == "new")
            {
                var feedback = _iAnswerRepository.GetAllAnswers(currentUserId, questionId, pageInfo);
                if (feedback.Success)
                {
                    vm = new AnswerListViewModel
                    {
                        Answers = feedback.Answers,
                        QuestionId = questionId,
                        CanAccept = canAccept,
                        PageNo = pageNo,
                        CurrentUserId = CurrentUser == null ? 0 : Convert.ToInt16(CurrentUser.UserInfo.Id),
                        Condition = "new",
                        TotalPage = Convert.ToInt16(Math.Ceiling((double)feedback.Total / pageInfo.PageSize))

                    };
                    return PartialView("_AnswerList", vm);
                }
            }
            else if (condition == "highScore")
            {
                var feedback = _iAnswerRepository.GetHighScoreAnswers(currentUserId, questionId, pageInfo);
                if (feedback.Success)
                {
                    vm = new AnswerListViewModel
                    {
                        Answers = feedback.Answers,
                        QuestionId = questionId,
                        CanAccept = canAccept,
                        PageNo = pageNo,
                        CurrentUserId = CurrentUser == null ? 0 : Convert.ToInt16(CurrentUser.UserInfo.Id),
                        Condition = "highScore",
                        TotalPage = Convert.ToInt16(Math.Ceiling((double)feedback.Total / pageInfo.PageSize)),
                    };
                    return PartialView("_AnswerList", vm);
                }
            }
            else if (condition == "accepted")
            {
                var feedback = _iAnswerRepository.GetAcceptedAnswer(currentUserId, questionId, pageInfo);
                if (feedback.Success)
                {
                    vm = new AnswerListViewModel
                    {
                        Answers = feedback.Answers,
                        QuestionId = questionId,
                        CanAccept = canAccept,
                        PageNo = pageNo,
                        CurrentUserId = CurrentUser == null ? 0 : Convert.ToInt16(CurrentUser.UserInfo.Id),
                        Condition = "accepted",
                        TotalPage = Convert.ToInt16(Math.Ceiling((double)feedback.Total / pageInfo.PageSize)),
                    };
                    return PartialView("_AnswerList", vm);
                }
            }
            return PartialView("_AnswerList");
        }

        //
        // GET: /Question/Details/5

        public ViewResult Details(int id)
        {
            Question question = ORMDbContext.Questions.SingleOrDefault(q => q.Id == id && !q.IsDeleted);
            if(question != null)
            {
                int currentUserId = CurrentUser == null ? 0 : Convert.ToInt16(CurrentUser.UserInfo.Id);
                var questionVote = ORMDbContext.VotedQuestions.SingleOrDefault(t => t.QuestionId == id && t.VotedById == currentUserId);
                bool isVotedByCurrentUser = questionVote != null;
                var questionReport = ORMDbContext.ReportedQuestions.SingleOrDefault(t => t.QuestionId == id && t.ReportedById == currentUserId);
                bool isReportdByCurrentUser = questionReport != null;
                var questionBookmark = ORMDbContext.QuestionBookmarks.SingleOrDefault(t => t.QuestionId == id && t.BookmarkById == currentUserId);
                bool isAddedBookmarkByCurrentUser = questionBookmark != null;
                question.IsVotedByCurrentUser = isVotedByCurrentUser;
                question.IsReportedByCurrentUser = isReportdByCurrentUser;
                question.IsAddedBookmarkByCurrentUser = isAddedBookmarkByCurrentUser;
                ViewBag.CurrentUserId = CurrentUser == null ? 0 : CurrentUser.UserInfo.Id;
                ViewBag.NumberOfGuides = ORMDbContext.Guides.Count(g => !g.IsDeleted && g.isApproved && g.ModelID == question.ModelId);
                ViewBag.NumberOfQuestions = ORMDbContext.Questions.Count(g => !g.IsDeleted && g.ModelId == question.ModelId);
                QuestionDetailsViewModel vm = new QuestionDetailsViewModel();
                vm.Question = question;
                vm.TopOlderQuestions =
                    ORMDbContext.Questions.Where(g => g.PostedDate < question.PostedDate).OrderByDescending(g => g.PostedDate).Take(10).ToList();
                return View(vm);
            }            
            //ViewBag.CurrentPage = "questionPage";
            return View();
        }

        //
        // GET: /Question/Create

        [PermissionAuthorize]
        public ActionResult Create(int modelId = 0)
        {
            if(modelId != 0)
            {
                var model = ORMDbContext.Models.SingleOrDefault(m => !m.IsDeleted && m.Id == modelId);
                if(model != null)
                {
                    var question = new Question
                                       {
                                           ModelId = modelId
                                       };
                    var Device_Models = from m in ORMDbContext.Models
                                        where m.ManufacturerId == model.ManufacturerId
                                        & m.DeviceId == model.DeviceId
                                        & !m.IsDeleted
                                        select new
                                        {
                                            m.Id,
                                            m.Name
                                        };
                    ViewData["Device_Name"] = new SelectList(ORMDbContext.Devices.Where(i => !i.IsDeleted), "Id", "Name", model.DeviceId).OrderBy(d => d.Text);
                    ViewData["Manufacturer_Name"] = new SelectList(ORMDbContext.Manufacturers.Where(i => !i.IsDeleted), "Id", "Name", model.ManufacturerId).OrderBy(m => m.Text);
                    ViewData["Model_Name"] = new SelectList(Device_Models.ToList(), "Id", "Name", question.Model).OrderBy(m => m.Text);
                    return View(question);
                }
            }
            ViewData["Device_Name"] = new SelectList(ORMDbContext.Devices.Where(i => !i.IsDeleted), "Id", "Name", null).OrderBy(d => d.Text);
            ViewData["Manufacturer_Name"] = new SelectList(ORMDbContext.Manufacturers.Where(i => !i.IsDeleted), "Id", "Name", null).OrderBy(m => m.Text);
            //ViewBag.CurrentPage = "questionPage";
            return View();
        }

        //
        // POST: /Question/Create

        [HttpPost]
        public ActionResult Create(Question question)
        {
            if (ModelState.IsValid)
            {
                question.TitleUrl = "someUrl";
                question.PostedById = CurrentUser.UserInfo.Id;
                question.ContentHtml = question.ContentHtml.GetSafeHtml(Constants.AllowedHtmlTags, Constants.AllowedHtmlAttributes);
                var feedback = _iQuestionRepository.InsertQuestion(question);
                return RedirectToAction("Details", new{id=feedback.Data.Id});
            }
            if(question.ModelId != null)
            {
                Model model = ORMDbContext.Models.Find(question.ModelId);
                var Device_Models = from m in ORMDbContext.Models
                                    where m.ManufacturerId == model.ManufacturerId
                                    & m.DeviceId == model.DeviceId
                                    & !m.IsDeleted
                                    select new
                                    {
                                        m.Id,
                                        m.Name
                                    };
                ViewData["Device_Name"] = new SelectList(ORMDbContext.Devices.Where(i => !i.IsDeleted), "Id", "Name", model.DeviceId).OrderBy(d => d.Text);
                ViewData["Manufacturer_Name"] = new SelectList(ORMDbContext.Manufacturers.Where(i => !i.IsDeleted), "Id", "Name", model.ManufacturerId).OrderBy(m => m.Text);
                ViewData["Model_Name"] = new SelectList(Device_Models.ToList(), "Id", "Name", question.Model).OrderBy(m => m.Text);
            }
            
            //ViewBag.CurrentPage = "questionPage";
            return View(question);
        }

        //
        // GET: /Question/Edit/5
        [PermissionAuthorize]
        public ActionResult Edit(int id)
        {
            
            Question question = ORMDbContext.Questions.Find(id);
            if (CurrentUser.UserInfo.Id != question.PostedById && !CurrentUser.IsInRole("Admin"))
                return View("CannotAccess");
            Model model = ORMDbContext.Models.Find(question.ModelId);
            var Device_Models = from m in ORMDbContext.Models
                                where m.ManufacturerId == model.ManufacturerId
                                & m.DeviceId == model.DeviceId
                                & !m.IsDeleted
                                select new
                                {
                                    m.Id,
                                    m.Name
                                };
            ViewData["Device_Name"] = new SelectList(ORMDbContext.Devices.Where(i => !i.IsDeleted), "Id", "Name", model.DeviceId).OrderBy(d => d.Text);
            ViewData["Manufacturer_Name"] = new SelectList(ORMDbContext.Manufacturers.Where(i => !i.IsDeleted), "Id", "Name", model.ManufacturerId).OrderBy(m => m.Text);
            ViewData["Model_Name"] = new SelectList(Device_Models.ToList(), "Id", "Name", question.Model).OrderBy(m => m.Text);
            //ViewBag.CurrentPage = "questionPage";
            return View(question);
        }

        //
        // POST: /Question/Edit/5

        [HttpPost]
        public ActionResult Edit(Question question)
        {
            if (ModelState.IsValid)
            {
                question.TitleUrl = "someUrl";
                question.ContentHtml = question.ContentHtml.GetSafeHtml(Constants.AllowedHtmlTags, Constants.AllowedHtmlAttributes);
                int currentUserId = CurrentUser == null ? 0 : Convert.ToInt16(CurrentUser.UserInfo.Id);
                question.UpdatedById = currentUserId;
                var feedback = _iQuestionRepository.UpdateQuestion(question);
                if(feedback.Success)
                {
                    //ViewBag.CurrentPage = "questionPage";
                    return RedirectToAction("Details", new { id = question.Id });
                }

            }
            Model model = ORMDbContext.Models.Find(question.ModelId);
            var Device_Models = from m in ORMDbContext.Models
                                where m.ManufacturerId == model.ManufacturerId
                                & m.DeviceId == model.DeviceId
                                & !m.IsDeleted
                                select new
                                {
                                    m.Id,
                                    m.Name
                                };
            ViewData["Device_Name"] = new SelectList(ORMDbContext.Devices.Where(i => !i.IsDeleted), "Id", "Name", model.DeviceId).OrderBy(d => d.Text);
            ViewData["Manufacturer_Name"] = new SelectList(ORMDbContext.Manufacturers.Where(i => !i.IsDeleted), "Id", "Name", model.ManufacturerId).OrderBy(d => d.Text);
            ViewData["Model_Name"] = new SelectList(Device_Models.ToList(), "Id", "Name", question.Model).OrderBy(d => d.Text);
            //ViewBag.CurrentPage = "questionPage";
            return View(question);
        }

        //
        // GET: /Question/Delete/5
        [PermissionAuthorize]
        public string Delete(int id)
        {
            try
            {
                Question question = ORMDbContext.Questions.Find(id);
                question.IsDeleted = true;
                ORMDbContext.SaveChanges();
                return "";
            }
            catch (Exception e)
            {
                return "Da co loi xay ra";
            }
        }

        ////
        //// POST: /Question/Delete/5

        //[HttpPost, ActionName("Delete")]
        //public ActionResult DeleteConfirmed(int id)
        //{
        //    Question question = ORMDbContext.Questions.Find(id);
        //    ORMDbContext.Questions.Remove(question);
        //    ORMDbContext.SaveChanges();
        //    //ViewBag.CurrentPage = "questionPage";
        //    return RedirectToAction("Index");
        //}
        public ActionResult ManufacturerNameChange(int Manufacturer_Id, int Device_Id)
        {
            var Device_Models = from m in ORMDbContext.Models
                                where m.ManufacturerId == Manufacturer_Id
                                & m.DeviceId == Device_Id
                                & !m.IsDeleted
                                orderby m.Name
                                select new
                                {
                                    m.Id,
                                    m.Name
                                };
            return Json(Device_Models.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult DeviceNameChange(int Manufacturer_Id, int Device_Id)
        {
            var Device_Models = from m in ORMDbContext.Models
                                where m.ManufacturerId == Manufacturer_Id
                                & m.DeviceId == Device_Id
                                & !m.IsDeleted
                                orderby m.Name
                                select new
                                {
                                    m.Id,
                                    m.Name
                                };
            return Json(Device_Models.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult UpdateManufacturerList(int Device_Id)
        {
            var queryString = "select distinct mf.* from Manufacturers as mf inner join Models as m on mf.Id = m.ManufacturerId where m.DeviceId = {0} and mf.IsDeleted = 'false'";
            var Manufacturers = (ORMDbContext as System.Data.Entity.Infrastructure.IObjectContextAdapter)
                              .ObjectContext.ExecuteStoreQuery<Manufacturer>(queryString, Device_Id).ToList(); 
            return Json(Manufacturers.ToList(), JsonRequestBehavior.AllowGet);
        }

        //Submit New Answer
        [PermissionAuthorize]
        [HttpPost]
        public string SubmitAnswer(Answer answer)
        {
            if (CurrentUser == null)
                return Resources.Messages.NeedLogin;
            List<Answer> answerList = new List<Answer>();
            if(ModelState.IsValid)
            {
                answer.PostedById = CurrentUser.UserInfo.Id;

                // Call Repository to perform insert
                var feedback = _iAnswerRepository.InsertAnswer(answer);
                if (feedback.Success)
                {
                    answerList.Add(feedback.Data);
                    return "true";
                }   
            }
            return "Bạn không thể để trống câu trả lời";
        }

        //Vote for an Answer
        [PermissionAuthorize]
        [HttpPost]
        public JsonResult PlusPointForAnAnswer(int AnswerId)
        {
                if(CurrentUser == null)
                    return Json(new { message = Resources.Messages.NeedLogin, newPoint = "" });
                var feedback = _iAnswerRepository.VoteAnswer(AnswerId, CurrentUser.UserInfo.Id);
                if (feedback.Success)
                {
                    return Json(new { message = "", newPoint =  feedback.Data.UpVotes});
                }
            return Json(new { message = feedback.Message, newPoint =  ""});
        }

        //Vote for an Question

        [PermissionAuthorize]
        [HttpPost]
        public JsonResult PlusPointForAnQuestion(int QuestionId)
        {
            if (CurrentUser == null)
                return Json(new { message = Resources.Messages.NeedLogin, newPoint = "" });
            var feedback = _iQuestionRepository.VoteQuestion(QuestionId, CurrentUser.UserInfo.Id);
            if (feedback.Success)
            {
                return Json(new { message = "", newPoint = feedback.Data.UpVotes });
            }
            return Json(new { message = feedback.Message, newPoint = "" });
        }

        //Report for an Question
        [PermissionAuthorize]
        [HttpPost]
        public JsonResult AddBookmarkAnQuestion(int QuestionId)
        {
            if (CurrentUser == null)
                return Json(new { message = Resources.Messages.NeedLogin});
            var feedback = _iQuestionRepository.AddBookmarkAnQuestion(QuestionId, CurrentUser.UserInfo.Id);
            if (feedback.Success)
            {
                return Json(new { message = ""});
            }
            return Json(new { message = feedback.Message});
        }

        //Add bookmark an Question
        [PermissionAuthorize]
        [HttpPost]
        public JsonResult ReportAnQuestion(int QuestionId, string Reason)
        {
            if (CurrentUser == null)
                return Json(new { message = Resources.Messages.NeedLogin });
            var feedback = _iQuestionRepository.ReportQuestion(QuestionId, Reason, CurrentUser.UserInfo.Id);
            if (feedback.Success)
            {
                return Json(new { message = "" });
            }
            return Json(new { message = feedback.Message });
        }

        //Accept An Answer
        [PermissionAuthorize]
        [HttpPost]
        public string AcceptAnAnswer(int QuestionId, int AnswerId)
        {
            var feedback1 = _iQuestionRepository.MarkQuestionAsResolved(QuestionId);
            var feedback2 = _iAnswerRepository.MarkAnswerAsAccepted(AnswerId);
            if (feedback1.Success && feedback2.Success)
            {
                return "true";
            }
            return "";
        }

        //Accept An Answer
        [PermissionAuthorize]
        [HttpPost]
        public JsonResult DeleteAnAnswer(int QuestionId, int AnswerId)
        {
            var answer = ORMDbContext.Answers.Single(i => i.Id == AnswerId);
            var feedback = _iAnswerRepository.DeleteAnAnswer(AnswerId);
            if (feedback.Success)
            {
                if (answer.IsAccepted)
                {
                    return Json(new { Success = "true", isAcceptedAnswer = "true" });
                }
                return Json(new { Success = "true", isAcceptedAnswer = "false" });
            }
            return Json(new { Success = "false", isAcceptedAnswer = "false" });
        }       
        
        public ActionResult GetQuestionBookmarksList(int pageNo = 1)
        {
            QuestionBookmarkListViewModel vm = null;
            List<Question> questionList = new List<Question>();
            int currentUserId = CurrentUser == null ? 0 : Convert.ToInt16(CurrentUser.UserInfo.Id);
            var pageInfo = new PageInfo(Convert.ToInt16(ConfigurationManager.AppSettings["QuestionBookmarksPageSize"]), pageNo);
                var feedback = _iQuestionRepository.GetQuestionBookmarks(currentUserId, pageInfo);
                if (feedback.Success)
                {
                    vm = new QuestionBookmarkListViewModel()
                    {
                        Questions = feedback.Questions,
                        CurrentUserId = currentUserId,
                        PageNo = pageNo,
                        TotalPage = Convert.ToInt16(Math.Ceiling((double)feedback.Total / pageInfo.PageSize))

                    };
                    return PartialView("_QuestionBookmarksList", vm);
                }

                return PartialView("_QuestionBookmarksList");
        }

        public ActionResult GetSuggestList(int modelId, string stringToSearch)
        {
            SuggestListViewModel vm = null;
            List<Question> questionList = new List<Question>();
            List<Guide> guideList = new List<Guide>();
            var feedback1 = _iQuestionRepository.GetSimilarQuestions(modelId, stringToSearch);
            var feedback2 = _iGuideRepository.GetSimilarGuides(modelId, stringToSearch);
            if (feedback1.Success && feedback2.Success)
            {
                vm = new SuggestListViewModel()
                {
                    Questions = feedback1.Questions,
                    Guides = feedback2.Guides,

                };
                return PartialView("_SuggestList", vm);
            }

            return PartialView("_SuggestList");
        } 

        protected override void Dispose(bool disposing)
        {
            ORMDbContext.Dispose();
            base.Dispose(disposing);
        }

    }
}
